const app = getApp();
const util = require('../../../utils/util.js');

Page({
  data: {
    userInfo: {},
    notificationEnabled: true,
    voiceEnabled: false,
    darkMode: false,
    fontSizeText: '标准',
    cacheSize: '12.5MB'
  },

  onLoad() {
    this.loadUserInfo();
    this.loadSettings();
  },

  loadUserInfo() {
    const userInfo = app.globalData.userInfo || {
      name: '张大爷',
      phone: '138****8888',
      avatar: 'https://dummyimage.com/240x240/cccccc/888888.png&text=Avatar'
    };
    this.setData({ userInfo });
  },

  loadSettings() {
    // 从本地存储加载设置
    const notificationEnabled = util.getStorage('notificationEnabled') !== false;
    const voiceEnabled = util.getStorage('voiceEnabled') === true;
    const darkMode = util.getStorage('darkMode') === true;
    const fontSize = util.getStorage('fontSize') || 'standard';
    
    const fontSizeMap = {
      small: '小',
      standard: '标准',
      large: '大',
      xlarge: '超大'
    };

    this.setData({
      notificationEnabled,
      voiceEnabled,
      darkMode,
      fontSizeText: fontSizeMap[fontSize]
    });
  },

  onChangeAvatar() {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const tempFilePath = res.tempFilePaths[0];
        // 实际应上传到服务器
        this.setData({
          'userInfo.avatar': tempFilePath
        });
        util.showToast('头像已更新', 'success');
      }
    });
  },

  onEditProfile() {
    wx.navigateTo({
      url: '/pages/mine/settings/edit-profile'
    });
  },

  onChangePassword() {
    wx.navigateTo({
      url: '/pages/mine/settings/change-password'
    });
  },

  onNotificationChange(e) {
    const enabled = e.detail.value;
    this.setData({
      notificationEnabled: enabled
    });
    util.setStorage('notificationEnabled', enabled);
    util.showToast(enabled ? '已开启消息通知' : '已关闭消息通知', 'success');
  },

  onVoiceChange(e) {
    const enabled = e.detail.value;
    this.setData({
      voiceEnabled: enabled
    });
    util.setStorage('voiceEnabled', enabled);
    util.showToast(enabled ? '已开启语音播报' : '已关闭语音播报', 'success');
  },

  onFontSizeSetting() {
    wx.showActionSheet({
      itemList: ['小', '标准', '大', '超大'],
      success: (res) => {
        const sizeMap = ['small', 'standard', 'large', 'xlarge'];
        const textMap = ['小', '标准', '大', '超大'];
        const fontSize = sizeMap[res.tapIndex];
        
        this.setData({
          fontSizeText: textMap[res.tapIndex]
        });
        util.setStorage('fontSize', fontSize);
        util.showToast('字体大小已设置', 'success');
      }
    });
  },

  onDarkModeChange(e) {
    const enabled = e.detail.value;
    this.setData({
      darkMode: enabled
    });
    util.setStorage('darkMode', enabled);
    util.showToast(enabled ? '已开启深色模式' : '已关闭深色模式', 'success');
  },

  onPrivacySettings() {
    wx.navigateTo({
      url: '/pages/mine/settings/privacy'
    });
  },

  onAccountSecurity() {
    wx.navigateTo({
      url: '/pages/mine/settings/security'
    });
  },

  onCheckUpdate() {
    util.showLoading('检查更新中...');
    setTimeout(() => {
      util.hideLoading();
      util.showToast('当前已是最新版本', 'success');
    }, 1000);
  },

  onClearCache() {
    util.showConfirm('确定要清除缓存吗？').then(confirm => {
      if (confirm) {
        util.showLoading('清除中...');
        setTimeout(() => {
          this.setData({
            cacheSize: '0MB'
          });
          util.hideLoading();
          util.showToast('缓存已清除', 'success');
        }, 1000);
      }
    });
  },

  onHelp() {
    wx.navigateTo({
      url: '/pages/mine/settings/help'
    });
  },

  onLogout() {
    util.showConfirm('确定要退出登录吗？').then(confirm => {
      if (confirm) {
        // 清除登录信息
        util.removeStorage('token');
        util.removeStorage('userInfo');
        app.globalData.userInfo = null;
        
        util.showToast('已退出登录', 'success');
        
        // 跳转到登录页或首页
        setTimeout(() => {
          wx.reLaunch({
            url: '/pages/index/index'
          });
        }, 1000);
      }
    });
  }
})

